package com.hp.interview.arithmetic;

/**
 * 判断活动时间是否重叠
 *
 * @author hupan
 * @date 2021-06-02 20:21
 */
public class TimeCoincidenceJudge {
    static byte[] arr = new byte[24 * 60];

    public static void addActivity(String s, String e) {
        int start = string2Int(s);
        int end = string2Int(e);

        if (start < end) {
            if (isCoincidence(start, end)) {
                System.out.printf("时间重合:%s-%s%n", s, e);
                return;
            }


            for (int i = start - 1; i < end - 1; i++) {
                arr[i] = 1;
            }
        }
    }

    public static boolean isCoincidence(int start, int end) {
        return arr[start - 1] == 1 || arr[end - 1] == 1;
    }

    public static int string2Int(String time) {
        String[] a = time.split(":");
        int h = Integer.parseInt(a[0]);
        int m = Integer.parseInt("00".equals(a[1]) ? "0" : a[1]);

        return h * 60 + m;
    }


    public static void main(String[] args) {
        String s1 = "9:00";
        String e1 = "10:00";

        String s2 = "10:30";
        String e2 = "12:00";

        String s3 = "14:00";
        String e3 = "15:30";

        String s4 = "14:30";
        String e4 = "15:00";

        addActivity(s1, e1);
        addActivity(s2, e2);
        addActivity(s3, e3);
        addActivity(s4, e4);
    }

}
